package lyc.springcloud.admin.service.impl.systemManagerServiceImpl;

import lombok.extern.slf4j.Slf4j;
import lyc.springcloud.admin.dao.systemManagerDao.ResourceManagerDao;
import lyc.springcloud.admin.dto.systemManagerDto.ResourceManagerDto;
import lyc.springcloud.admin.service.def.systemManagerService.ResourceManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@Slf4j
public class ResourceManagerServiceImpl implements ResourceManagerService {
    @Autowired
    private ResourceManagerDao resourceManagerDao;

    /**
     * 资源管理---查询所有资源tree（包括菜单，不包括功能）
     * */
    @Override
    public Map<String, Object> selectResourceTree() {
        String root_id = "root_node";
        // 所有资源tree
        Map<String, Object> rootTree = new HashMap<>();
        // root下所有资源tree
        List<Map> resourceTreeList = new ArrayList<Map>();
        // 查询所有资源---平级数据
        List<Map> allResourceList = resourceManagerDao.selectAllResource();
        // 一级资源---平铺数据
        List<Map> all_level1_resourceList = new ArrayList<Map>();

        // 搞定一级菜单(menu不需要展示root级的菜单，进来只看到收起的所有一级菜单)
        for (Map item : allResourceList) {
            String parentId = (String) item.get("parentId");
            if (parentId.equals(root_id)) {
                all_level1_resourceList.add(item);
            }
        }
        // 一级菜单---加children属性
        for (Map item : all_level1_resourceList) {
            item.put("children", getRestNode((String) item.get("resourceId"), allResourceList));
        }

        // 生成menuTree菜单
        resourceTreeList.addAll(all_level1_resourceList);

        // 最终生成roottree
        rootTree.put("resourceId", "root_node");
        rootTree.put("title", "全部资源");
        rootTree.put("isLeaf", 0);
        rootTree.put("children", resourceTreeList);
        return rootTree;
    }

    private List<Map> getRestNode(String parentId, List<Map> allMenuList) {
        // 也许这种递归写法可以支持n级的tree分支，但前端的iview组件menu多一级就要多写一层dom，目前3级
        List<Map> restNodeList = new ArrayList<Map>();
        for (Map item : allMenuList) {
            String pId = (String) item.get("parentId");
            if(pId.equals(parentId)) {
                restNodeList.add(item);
            }
        }
        for(Map item : restNodeList) {
            String pId = (String) item.get("parentId");
            item.put("children", getRestNode((String) item.get("resourceId"), allMenuList));
        }
        // 递归退出条件
        if (restNodeList.size() == 0) {
            return null;
        }
        return restNodeList;
    }

    /**
     * 资源管理---根据resourceId查询下一级子节点
     * */
    @Override
    public Map<String, Object> selectChildByResourceId(ResourceManagerDto params) {
        params.setPageNum((params.getPageNum()-1) * params.getPageSize());
        Map<String, Object> map = new HashMap<>();
        List<ResourceManagerDto> list = resourceManagerDao.selectChildByResourceId(params);
        map.put("list", list);
        map.put("total", resourceManagerDao.countOnChildrenById(params));
        return map;
    }

    /**
     * 资源管理---新增
     * */
    @Override
    public void resourceMng_insert(ResourceManagerDto params){
        params.setResourceId(UUID.randomUUID().toString().replaceAll("-", ""));
        resourceManagerDao.resourceMng_insert(params);
    }

    /**
     * 资源管理---编辑
     * */
    @Override
    public void resourceMng_update(ResourceManagerDto params){
        log.info(String.valueOf(params));
        resourceManagerDao.resourceMng_update(params);
    }

    /**
     * 资源管理---删除
     * */
    @Override
    public void resourceMng_delete(String params){
        List list = Arrays.asList(params.split(","));
        resourceManagerDao.resourceMng_delete(list);
    }
}
